package com.example.helllo;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

public class ConfigActivity2 extends AppCompatActivity {
    private  static final String TAG="ConfigActivity";
    EditText inpDR;
    EditText inpER;
    EditText inpWR;
    float valueD;
    float valueE;
    float valueW;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config2);

        Intent intent= getIntent();//接收获得Exchange中的数据
        float dollar2=intent.getFloatExtra("dollar_key",0.0f);//取数据，标签一致
        float euro2=intent.getFloatExtra("euro_key",0.0f);
        float won2=intent.getFloatExtra("won_key",0.0f);

        Log.i(TAG,"onCreate: dollar2=" + dollar2);
        Log.i(TAG,"onCreate: euro2=" + euro2);
        Log.i(TAG,"onCreate: won2=" + won2);
        //获取控件
        inpDR=findViewById(R.id.inpDR);
        inpER=findViewById(R.id.inpER);
        inpWR=findViewById(R.id.inpWR);
        //将传入的数据显示到控件，不放的话就会为空报错
        inpDR.setText(String.valueOf(dollar2));
        inpER.setText(String.valueOf(euro2));
        inpWR.setText(String.valueOf(won2));
    }
    public void Config1(View btn){
        //获取输入数据
        String dollarRate=inpDR.getText().toString();
        String euroRate=inpER.getText().toString();
        String wonRate=inpWR.getText().toString();
        //强制转换
        valueD = Float.valueOf(dollarRate);
        valueE = Float.valueOf(euroRate);
        valueW = Float.valueOf(wonRate);
//        float valueD=Float.parseFloat(inpDR.getText().toString());
//        float valueE=Float.parseFloat(inpER.getText().toString());
//        float valueW=Float.parseFloat(inpWR.getText().toString());
        Intent ret=getIntent();
        Bundle bdl=new Bundle();//将变量打包
        bdl.putFloat("dollar_key2",valueD);
        bdl.putFloat("euro_key2",valueE);
        bdl.putFloat("won_key2",valueW);
        ret.putExtras(bdl);//把包里的数据放到ret里
        setResult(2,ret);//2是相应代码
        //保存获取的汇率数据
        SharedPreferences sp=getSharedPreferences("myrate", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putFloat("dollar_rate", Float.parseFloat(dollarRate));
        editor.putFloat("euro_rate", Float.parseFloat(euroRate));
        editor.putFloat("won_rate", Float.parseFloat(wonRate));
        editor.apply();
        //返回到调用窗口
        finish();
    }
}